package com.issc.framework.bean;

import com.issc.framework.util.CastUtil;
import com.issc.framework.util.StringUtil;
import net.sf.cglib.core.CollectionUtils;
import org.apache.commons.fileupload.FileItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Param {
    private Map<String,Object> paramMap;
    private List<FileParam> fileParamList;
    private List<FormParam> formParamList;

    public Param(List<FormParam> formParamList) {
        this.formParamList = formParamList;
    }

    public Param(List<FileParam> fileParamList, List<FormParam> formParamList) {
        this.fileParamList = fileParamList;
        this.formParamList = formParamList;
    }

    public Param(Map<String, Object> paramMap) {
        this.paramMap = paramMap;
    }

    public Map<String, Object> getParamMap() {
        return paramMap;
    }

    /**
     * 请求参数
     * @return
     */
    public Map<String, Object> getFieldMap() {
        Map<String, Object> fieldMap = new HashMap<>();
        if (!formParamList.isEmpty()) {
            for (FormParam formParam : formParamList) {
                String fieldMapName=formParam.getFieldName();
                Object fieldMapValue=formParam.getFieldValue();
                if (fieldMap.containsKey(fieldMapName)) {
                    fieldMapValue=fieldMap.get(fieldMapName)+ StringUtil.SEPARATOR+fieldMapValue;
                }
                fieldMap.put(fieldMapName, fieldMapValue);
            }
        }
        return fieldMap;
    }

    /**
     * 获取上传文件参数映射
     * @return
     */
    public Map<String, List<FileParam>> getFileMap() {
        Map<String, List<FileParam>> fileMap = new HashMap<>();
        if (!fileParamList.isEmpty()){
            for (FileParam fileParam : fileParamList) {
                String fieldName=fileParam.getFieldName();
                List<FileParam> fileParamList;
                if (fileMap.containsKey(fieldName)) {
                    fileParamList = fileMap.get(fieldName);
                } else {
                    fileParamList = new ArrayList<>();
                }
                fileParamList.add(fileParam);
                fileMap.put(fieldName, fileParamList);
            }
        }
        return fileMap;
    }

    /**
     * 获取相关的所有文件
     * @param fileName
     * @return
     */
    public List<FileParam> getFileList(String fileName) {
        return getFileMap().get(fileName);
    }

    public long getLong(String name){
        return CastUtil.castLong(paramMap.get(name));
    }

    /**
     * 获取单个文件
     * @param fileName
     * @return
     */
    public FileParam getFile(String fileName){
        List<FileParam> fileParamList = getFileList(fileName);
        if (!fileParamList.isEmpty() && fileParamList.size() == 1) {
            return  fileParamList.get(0);
        }else {
            return null;
        }
        }


    public String getString(String name) {
        return CastUtil.castString(getFieldMap().get(name));
    }

    public double getDouble(String name) {
        return CastUtil.castDouble(getFieldMap().get(name));
    }

    public int getInt(String name) {
        return CastUtil.castInt(getFieldMap().get(name));
    }

    public boolean getBoolean(String name) {
        return CastUtil.castBoolean(getFieldMap().get(name));
    }

       // Object paramValue=paramMap.get(name);
//        if (paramValue!=null&&paramValue instanceof List){
//            return  (FileItem) ((List) paramValue).get(0);
//        }else{
//            return  null;
//            //throw new RuntimeException("there is no file!");
//        }
//    }
}
